/*
 *  $TEAM_NAME Space Trader
 */
package presenters;

import models.Player;
import models.Marketplace;
import models.Ship;
import models.TradeGood;
import views.MarketPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.Observer;
import java.util.Observable;

/**
 * MarketPresenter handles the presentation of the market panel 
 * and the interaction of the market
 * @author bradley
 * @version 1.0
 */
public class MarketPresenter extends Presenter<MarketPanel> 
implements ActionListener, ChangeListener, Observer {
	/**
	 * field
	 */
	private final Player player;

	/**
	 * field
	 */
	private Marketplace marketplace;
    
	/**
	 * field
	 */
	private Ship ship;
	
	/**
	 * Creates a new market presenter 
	 * @param player the player
	 * @param view the market panel
	 */
	public MarketPresenter(Player player, MarketPanel view) {
        this.marketplace = player.getLocation().getMarketplace(); 
        this.ship = player.getShip();
        this.player = player;

        view.addActionListener(this);
        super.setView(view);

        player.addObserver(this);
        ship.addObserver(this);
        marketplace.addObserver(this);
    }

	/**
	 * action performed
	 * @param e
	 */
    @Override
    public void actionPerformed(ActionEvent e) {
        final Object source = e.getSource();

        if (source instanceof JButton) {
            final JButton button = (JButton) source;

            final TradeGood good = view.getTradeGood();
            final int amount = view.getAmount();
            final int cost = marketplace.getPrice(good) * amount;

            if (amount == -1) {
                JOptionPane.showMessageDialog(frame, "Amount must be an integer!");
                System.out.println("Amount must be an integer!");

                return;
            }

            if ("Buy".equals(button.getText())) {
                if (amount <= marketplace.getQuantity(good)) {
                    if (amount <= ship.getRemainingCargoCapacity()) {
                        if (cost <= player.getCredits()) {
                            if(player.useCredits(cost) == 0){
                            	System.out.print("");
                            }
                            if(ship.canAddTradeGood(good, amount)){
                            	System.out.print("");
                            }
                            if(marketplace.canBuy(good, amount)){
                            	System.out.print("");
                            }

                            player.notifyObservers();
                            ship.notifyObservers();
                            marketplace.notifyObservers();
                        } else {
                            JOptionPane.showMessageDialog(
                            		frame, "You don't have enough money for that!");
                            System.out.println("You don't have enough money for that!");
                        }
                    } else {
                        //TODO maybe print the actual remaining space also
                        JOptionPane.showMessageDialog(
                        		frame, "Not enough room in cargohold");
                        System.out.println("Not enough room in cargohold");
                    }
                } else {
                    JOptionPane.showMessageDialog(
                    		frame, "The market doesn't have that many.");
                    System.out.println("The market doesn't have that many.");
                }
            } else if ("Sell".equals(button.getText())) {
                if (amount <= ship.getTradeGoodQuantity(good)) {
                    if(player.addCredits(cost) == 0){
                    	System.out.print("");
                    }
                    if(ship.canRemoveTradeGood(good, amount)){
                    	System.out.print("");
                    }
                    if(marketplace.canSell(good, amount)){
                    	System.out.print("");
                    }

                    player.notifyObservers();
                    ship.notifyObservers();
                    marketplace.notifyObservers();
                } else {
                    JOptionPane.showMessageDialog(frame, "You do not have enough.");
                    System.out.println("You do not have enough.");
                }
            }
        }
    }

    /**
     * update
     * @param o
     * @param arg
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o.equals(player)) {
            marketplace = player.getLocation().getMarketplace();
            ship = player.getShip();
        }

        view.update();
    }
    
    /**
     * state changed
     * @param ce
     */
    @Override
    public void stateChanged(ChangeEvent ce) {
    	System.out.println(ce);
    }
    
    /**
     * to string
     * @return string
     */
    @Override
    public String toString() {
		return "";
	}
}
